package com.cdlj.moes.service;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

import com.cdlj.moes.common.Constants;
import com.cdlj.moes.common.UserInfo;

public class Service {
	
	public static String registUser(UserInfo mUserInfo) {
        Log.d(Constants.APP_TAG, "Service.registUser()");
        String serverURL =  Constants.MOES_SERVER_URL+Constants.SIGN_IN_URL;
        Log.d(Constants.APP_TAG, "serverURL"+serverURL);
        String respStr = "";
        try {
            // Set connection parameter
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, Constants.APP_CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpParams, Constants.APP_SOCKET_TIMEOUT);
            HttpClient httpClient = new DefaultHttpClient(httpParams);

            HttpPost httpPost = new HttpPost(serverURL);
            // Set parameter
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart(Constants.USER_NAME, new StringBody(mUserInfo.getUserName()));
            reqEntity.addPart(Constants.PASSWORD, new StringBody(mUserInfo.getPassword()));
            reqEntity.addPart(Constants.PERMISSION_ID, new StringBody(mUserInfo.getPermissionId()));
            reqEntity.addPart(Constants.EMAIL, new StringBody(mUserInfo.getEmail()));
            reqEntity.addPart(Constants.PHONE, new StringBody(mUserInfo.getPhoneNumber()));
            reqEntity.addPart(Constants.ADDRESS, new StringBody(mUserInfo.getAddress()));
            httpPost.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(httpPost);
            // Handle response
            HttpEntity respEntity = response.getEntity();
            respStr = EntityUtils.toString(respEntity);
            if (respEntity != null) {
                respEntity.consumeContent();
            }
            // Close connection
            httpClient.getConnectionManager().shutdown();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(Constants.APP_TAG, "Service.registUser errorMessage:" + e.getMessage());
        }
        return respStr;
    }
	
	public static String userLogin(String username,String passsword) {
        Log.d(Constants.APP_TAG, "Service.registUser()");
        String serverURL =  Constants.MOES_SERVER_URL+Constants.LOGIN_URL;
        Log.d(Constants.APP_TAG, "serverURL"+serverURL);
        String respStr = "";
        try {
            // Set connection parameter
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, Constants.APP_CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpParams, Constants.APP_SOCKET_TIMEOUT);
            HttpClient httpClient = new DefaultHttpClient(httpParams);

            HttpPost httpPost = new HttpPost(serverURL);
            // Set parameter
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart(Constants.USER_NAME, new StringBody(username));
            reqEntity.addPart(Constants.PASSWORD, new StringBody(passsword));
           // reqEntity.addPart(Constants.PERMISSION_ID, new StringBody(permission));
            httpPost.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(httpPost);
            // Handle response
            HttpEntity respEntity = response.getEntity();
            respStr = EntityUtils.toString(respEntity);
            if (respEntity != null) {
                respEntity.consumeContent();
            }
            // Close connection
            httpClient.getConnectionManager().shutdown();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(Constants.APP_TAG, "Service.registUser errorMessage:" + e.getMessage());
        }
        return respStr;
    }

	
	

}
